<?php
namespace app\admin\controller;

use app\common\model\ChangguanFeiyong;
use app\common\model\Changguan;
use app\common\model\ChangguanJinyong;
use app\common\model\ChangguanYuyue as ChangGuanYuyueModel;
use app\common\model\ChangguanFenleiChangdi;
use app\common\model\ChangguanChangdi;

use zjkal\ChinaHoliday;

class ChangguanYuyue extends Base
{
    public function __construct () {
        parent::__construct();
    }

    /**
     * 列表
     * @return \think\response\Json
     */
    public function list(){

        //查询条件
        $where = array();

        if (input('name')) {
            $where['name'] = array('like', "%" . input('name') . "%");
        }

        //分页信息
        $page = max(input('page'), 1);
        $size = input('limit',20);

        $list = ChangGuanYuyueModel::getPage($where, $page, $size);

        //总页码
        $total = ChangGuanYuyueModel::getCount($where);

        //数据返回
        if (!$list) {
            return returnJson(0, '', array('list' => [], 'total' => 0));
        }
        $fl_cd = ChangguanFenleiChangdi::getColumn([],'id,label');
        $cg = Changguan::getColumn([],'id,name');
        foreach ($list as &$v){
            $v['name'] = isset($cg[$v['cgid']])?$cg[$v['cgid']]:'';
            $v['fenlei'] = isset($fl_cd[$v['flid']])?$fl_cd[$v['flid']]:'';
            $v['changdi'] = isset($fl_cd[$v['cdid']])?$fl_cd[$v['cdid']]:'';
        }

        return returnJson(0, '', [
            'list' => $list,
            'total' => $total
        ]);
    }

    /**
     * 新增、编辑
     * @return \think\response\Json
     */
    public function edit(){

        if(request()->isPost()){

            $price  = input('price/a');
            $save = [];
            foreach ($price as $k=>$v){
                $save[] = array(
                    'cgid'=>input('cgid'),
                    'status'=>input('status'),
                    'price'=>$v['price'],
                    'start_time'=>$v['start_time'],
                    'end_time'=>$v['end_time'],
                    'create_time'=>time(),
                    'update_time'=>time()
                );
            }
            ChangguanFeiyong::setDel(array('cgid'=>input('cgid'),'status'=>input('status')));
            ChangguanFeiyong::setSaveAll($save);
            return returnJson(0,'success');
        }
        //返回
        return returnJson(0,'');
    }

    /**
     * 列表
     * @return \think\response\Json
     */
    public function detail(){

        //查询条件
        $where = array(
            'cgid'=>input('cgid'),
            'status'=>ChinaHoliday::isHoliday(input('date'))?2:1
        );



        //所有时段
        $time = ChangguanFeiyong::getAll($where,'start_time asc','start_time,end_time,price');

        if(!$time){
            return returnJson(0, '', $time);
        }

        //长期禁用
        $where['status'] = 1;
        $where['week'] = date('w',strtotime(input('date')));
        $changqi_jinyong = ChangguanJinyong::getAll($where);

        //一次性禁用
        unset($where['week']);
        $where['date'] = input('date');
        $where['status'] = 2;
        $one_jinyong = ChangguanJinyong::getAll($where);

        //预约
        unset($where['status']);
        $where['checked'] = 1;
        $yuyue = ChangGuanYuyueModel::getAll($where);


        $fl_cd_ids = ChangguanChangdi::getColumn(array('cgid'=>input('cgid')),'id,flid,cdid');
        $fl_cd = ChangguanFenleiChangdi::getColumn([],'id,label');

        foreach ($fl_cd_ids as $v){
            $data[$v['flid']]['name'] = isset($fl_cd[$v['flid']])?$fl_cd[$v['flid']]:'';
            foreach ($time as &$t){
                $v['jinyong'] = false;
                if($one_jinyong){
                    foreach ($one_jinyong as $k=>$vv){
                        if($vv['start_time']==$t['start_time']){
                            $t['jinyong'] = true;
                        }
                    }
                }
                if($changqi_jinyong){
                    foreach ($changqi_jinyong as $k=>$vv){
                        if($vv['start_time']==$t['start_time']){
                            $t['jinyong'] = true;
                        }
                    }
                }
                $v['yuyue'] = false;
                if($yuyue){
                    foreach ($yuyue as $k=>$vv){
                        if($t['start_time']>=$vv['start_time'] && $t['end_time']<=$vv['end_time'] && $v['cdid']==$vv['cdid']){
                            $t['yuyue'] = true;
                        }
                    }
                }
            }
            $data[$v['flid']]['changdi'][] = [
                'id'=>$v['cdid'],
                'name' =>isset($fl_cd[$v['cdid']])?$fl_cd[$v['cdid']]:'',
                'time'=>$time
            ];
        }



        return returnJson(0, '', compact('data','time'));
    }



}
